$in_path=$ARGV[0];
my @out_cpg;
my @out_cytosine;
my $cpg_found = 0;
my $cytosine_found = 0;
my $cpg_non_ref = 0;
my $cytosine_non_ref = 0;
my $ref_cytosine_count = 0;
my $ref_cytosine_in_found_count = 0;
my $cpg_name_output = $in_path."/normalMerge.cpg.nonref.hg18.gtf";
my $cytosine_name_output = $in_path."/normalMerge.cytosine.nonref.hg18.gtf";
opendir(DH,"$in_path") or die;
foreach my $file(readdir(DH)){
	next if $file =~ /^\./ ;
	$file = $in_path.$file;
	open(FH,"<$file") or die;
	my @in=<FH>;
	chomp(@in);
	close(FH);
	foreach my $line(@in){
		next if $line =~ /^#/;
		my @splitin = split "\t", $line;
		if($splitin[3] eq "G" or $splitin[3] eq "C"){
			$ref_cytosine_count++;
		}
		if($splitin[6] eq "PASS"){
			if($splitin[3] eq "G" or $splitin[3] eq "C"){
				$ref_cytosine_in_found_count++;
			}
			
			if($splitin[3] ne "G" and $splitin[3] ne "C"){
				if($splitin[7] =~ /CType/){
					$cytosine_non_ref++;
					my $start = $splitin[1];
					my $end = $splitin[1] + 1;
					push(@out_cytosine,"$splitin[0]\tnormalMerge\texon\t$start\t$end\t0.0\t\+\t0\n");
				}
				if($splitin[7] =~ /CG/){
					$cpg_non_ref++;
					my $start = $splitin[1];
					my $end = $splitin[1] + 1;
					push(@out_cpg,"$splitin[0]\tnormalMerge\texon\t$start\t$end\t0.0\t\+\t0\n");
				}
			}
			elsif($splitin[3] eq "C" and $splitin[7] =~ /NegC/){
				if($splitin[7] =~ /CType/){
					$cytosine_non_ref++;
					my $start = $splitin[1];
					my $end = $splitin[1] + 1;
					push(@out_cytosine,"$splitin[0]\tnormalMerge\texon\t$start\t$end\t0.0\t\+\t0\n");
				}
				if($splitin[7] =~ /CG/){
					$cpg_non_ref++;
					my $start = $splitin[1];
					my $end = $splitin[1] + 1;
					push(@out_cpg,"$splitin[0]\tnormalMerge\texon\t$start\t$end\t0.0\t\+\t0\n");
				}
			}
			elsif($splitin[3] eq "G" and $splitin[7] !~ /NegC/){
				if($splitin[7] =~ /CType/){
					$cytosine_non_ref++;
					my $start = $splitin[1];
					my $end = $splitin[1] + 1;
					push(@out_cytosine,"$splitin[0]\tnormalMerge\texon\t$start\t$end\t0.0\t\+\t0\n");
				}
				if($splitin[7] =~ /CG/){
					$cpg_non_ref++;
					my $start = $splitin[1];
					my $end = $splitin[1] + 1;
					push(@out_cpg,"$splitin[0]\tnormalMerge\texon\t$start\t$end\t0.0\t\+\t0\n");
				}
			}
			
			if($splitin[7] =~ /CType/){
				$cytosine_found++;
			}
			if($splitin[7] =~ /CG/){
				$cpg_found++;
			}
		}
	}
	print("$file\n");
	print("cpg_found: $cpg_found\n");
	print("cpg_non_ref: $cpg_non_ref\n");
	print("cytosine_found: $cytosine_found\n");
	print("cytosine_non_ref: $cytosine_non_ref\n");
	print("ref_cytosine_count: $ref_cytosine_count\n");
	print("ref_cytosine_in_found_count: $ref_cytosine_in_found_count\n");
}
open(OUT,">$cpg_name_output") or die;
print OUT @out_cpg;
close(OUT);
open(OUT,">$cytosine_name_output") or die;
print OUT @out_cytosine;
close(OUT);
print("cpg_found: $cpg_found\n");
print("cpg_non_ref: $cpg_non_ref\n");
print("cytosine_found: $cytosine_found\n");
print("cytosine_non_ref: $cytosine_non_ref\n");
print("ref_cytosine_count: $ref_cytosine_count\n");
print("ref_cytosine_in_found_count: $ref_cytosine_in_found_count\n");


